package es.unileon.prg2.censo.singleton;

/**
 * @author Manuel Molinos Crespo, Adrian Campazas Vega, Alberto Castro Delgado, Pedro Alvarez Alvarez, Adrian Cabello Galvan, Jose Alvaro Fernandez Alvarez
 *
 */

import java.util.Hashtable;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

public class TableComprobation {
	static Logger logger = Logger.getLogger(TableComprobation.class.getName());
	
	private Hashtable<String, Boolean> comparation;
	private static TableComprobation singletonTable = null;

	/**
	 * Private constructor for preventing instantiation from other classes.
	 */
	private TableComprobation() {
		PropertyConfigurator.configure("etc/log4j.properties");
		this.comparation = new Hashtable<String, Boolean>();
		comparation.put("CountryRegion", true);
		comparation.put("CountryCitizen", true);
		comparation.put("RegionProvince", true);
		comparation.put("RegionCitizen", true);
		comparation.put("ProvinceTown", true);
		comparation.put("ProvinceCitizen", true);
		comparation.put("TownCitizen", true);
		logger.info(comparation.toString()+" has been created succesfully.");
	}

	/**
	 * Method used to order creating of the TableComprobation constructor if it hasn't been created before, and else, return the TableComprobation. 
	 * @return The TableComprobation that has been created
	 */
	public static TableComprobation getInstance() {
		if (singletonTable == null) {
			singletonTable = new TableComprobation();
			logger.info(singletonTable.toString()+" has been created succesfully.");
		}
		logger.info(singletonTable.toString()+" has been returned succesfully.");
		return singletonTable;
	}

	/**
	 * Method used to check if the first parameter can created the second parameter.
	 * @param type1 String with the type of component that build the other component
	 * @param type2 String with the type of component that try creating
	 * @return If the checkout stay into the hashtable return true, else return false
	 */
	public boolean comprobation(String type1, String type2) {
		if (comparation.get(type1 + type2) != null) {
			logger.info(comparation.toString()+" has been returned true.");
			return true;
		} else {
			logger.info(comparation.toString()+" has been returned false.");
			return false;
		}

	}
}
